//
//  CustomTabBarViewController.m
//  ViewAnimation
//
//  Created by Billy on 15/5/21.
//  Copyright (c) 2015年 seven. All rights reserved.
//

#import "CustomTabBarViewController.h"
#import "MyAccountViewController.h"
#import "MoreSettingsViewController.h"
#import "TicketsViewController.h"
#import "WalletViewController.h"

@interface CustomTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation CustomTabBarViewController

#pragma mark - custom implementation

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self setDelegate:self];

    [self subControllersCreation];
}

-(void)viewWillAppear:(BOOL)animated {
    
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
    
}

-(void) viewDidAppear:(BOOL)animated{
    
    [self.selectedViewController endAppearanceTransition];

}

-(void) viewWillDisappear:(BOOL)animated{
    
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated{
    
    [self.selectedViewController endAppearanceTransition];
}

#pragma mark - SubControllerCreation

- (void) subControllersCreation {
    
    
    // 牛钱包
    WalletViewController *walletVC = [[WalletViewController alloc] init];
    [self setSubController:walletVC withTitle:nil
                                                                andDefautImageName:@"walletNew"
                                                                andSelectedImageName:@"walletSelected"];
    
    //加息券
    TicketsViewController *ticketVC = [[TicketsViewController alloc] init];
    [self setSubController:ticketVC withTitle:nil
                                                                andDefautImageName:@"ticket"
                                                                andSelectedImageName:@"ticketSelected"];
    
    //我的账户
    MyAccountViewController *accountVC = [[MyAccountViewController alloc] init];
    [self setSubController:accountVC withTitle:nil
        andDefautImageName:@"account"
      andSelectedImageName:@"accountSelected"];
    
    
    // 更多
    
    MoreSettingsViewController *settingVC = [[MoreSettingsViewController alloc] init];
    [self setSubController:settingVC withTitle:nil
        andDefautImageName:@"more"
      andSelectedImageName:@"moreSelected"];
    
}

- (void) setSubController:(UIViewController *) subViewController withTitle:(NSString *) title
                                            andDefautImageName:(NSString *) defaultImageName
                                            andSelectedImageName:(NSString *) selectionImageName{
    
    [subViewController.tabBarItem setTitle:title];
    
    if ([subViewController isKindOfClass:[TicketsViewController class]]){
        
        [subViewController.tabBarItem setImageInsets:UIEdgeInsetsMake(8, 0, -8, 0)];
    } else if ([subViewController isKindOfClass:[MoreSettingsViewController class]]) {
        
        [subViewController.tabBarItem setImageInsets:UIEdgeInsetsMake(11.5, 0, -11.5, 0)];
    } else {
        
        [subViewController.tabBarItem setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
    }

    
    
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.1) {
        
        [subViewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:selectionImageName] withFinishedUnselectedImage:[UIImage imageNamed:defaultImageName]];
        
    } else {
        
        subViewController.tabBarItem.image = [UIImage imageNamed:defaultImageName];
        subViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectionImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:subViewController];
    
    [self addChildViewController:nav];
}

#pragma mark - TabBar delegate



//用来定位用户选择的item选择
- (void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController {
  
    
}

@end
